<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="6" :sm="24">
            <a-form-item label="终端标识">
              <a-input v-model="queryParam.dto.clientId" placeholder="" />
            </a-form-item>
          </a-col>

          <a-col :md="6" :sm="24">
            <span
              class="table-page-search-submitButtons"
              :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
              <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
              <a-button style="margin-left: 8px" @click="resetSearchForm()">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <s-table
      ref="table"
      size="small"
      bordered
      rowKey="clientId"
      :columns="columns"
      :data="loadData">
      <span slot="action" slot-scope="text, record">
        <template>
          <a-tag color="orange" @click="handleEdit(record)">设置秘钥</a-tag>
          <a-tag color="red" @click="handleResource(record)">接口授权</a-tag>
        </template>
      </span>
    </s-table>
    <form-handle ref="formHandle" @ok="handleOk" />
    <handle-resource ref="handleResource" @ok="handleOk" />
  </a-card>
</template>

<script>
  import formHandle from './handle'
  import handleResource from './handleResource'
  import {
    page,
    delByPk
  } from '@/api/system/clients'
  import {
    STable
  } from '@/components'
  export default {
    name: 'TableList',
    components: {
      STable,
      formHandle,
      handleResource
    },
    props: {
      statusFilter: {
        type: Function,
        default: undefined
      }
    },
    data () {
      return {
        mdl: {},
        advanced: false,
        queryParam: {
          dto: {}
        },
        columns: [{
            title: '终端标识',
            dataIndex: 'clientId'
          },
          {
            title: 'TOKEN有效期',
            dataIndex: 'accessTokenValidity'
          },
          {
            title: '操作',
            dataIndex: 'action',
            width: '250px',
            scopedSlots: {
              customRender: 'action'
            }
          }
        ],
        loadData: parameter => {
          return page(Object.assign(parameter, this.queryParam)).then(response => {
            return response.res
          })
        }
      }
    },
    created () {},
    methods: {
      handleDelete (record) {
        const that = this
        this.$confirm({
          title: '操作提醒',
          content: '确定要删除吗?',
          onOk () {
            delByPk({
              id: record.id
            }).then(response => {
              that.$message.info(response.message)
              that.$refs.table.refresh()
            }).catch((response) => {
              that.$message.warn(response.message)
            })
          },
          onCancel () {}
        })
      },
      handleEdit (record) {
        this.$refs.formHandle.edit(record)
      },
      handleResource (record) {
        this.$refs.handleResource.assignResource(record)
      },
      handleOk () {
        this.$refs.table.refresh()
      },
      resetSearchForm () {
        this.queryParam = {
          dto: {}
        }
        this.handleOk()
      }
    }
  }
</script>
